package com.gxa.community.core.utils;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
public class R extends HashMap<String, Object> {
    private Integer code;
    private String msg;
    private static final long serialVersionUID = 1L;

    public R() {
        put("code", 200);
        put("msg", "操作成功");
    }

    public static R error() {
        return error(500, "未知异常，请联系管理员");
    }

    public static R error(String msg) {
        return error(500, msg);
    }

    public static R error(int code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public static R ok(String msg) {
        R r = new R();
        r.put("msg", msg);
        return r;
    }

    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.putAll(map);
        return r;
    }

    public static R ok() {
        return new R();
    }

    @Override
    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    /**
     * 返回分页数据额
     *
     * @param page  当前页数
     * @param size  每页数量
     * @param total 数据总条数
     * @param list  数据集合
     */
    public static R page(Long page, Long size, Long total, Object list) {
        HashMap<String, Object> data = new HashMap<>();
        data.put("page", page);
        data.put("size", size);
        data.put("total", total);
        data.put("list", list);
        return R.ok().put("data", data);
    }
}
